<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2019/9/18
 * Time: 14:41
 */
namespace app\api\controller;
use think\Db;
use org\Qiniu;
class Timing extends Base {

    /**
     * 下载
     */
    public function dowload() {
        $agent = strtolower($_SERVER['HTTP_USER_AGENT']);
        if(strpos($agent, 'iphone') || strpos($agent, 'ipad'))
        {
            echo "<script> location.href='https://itunes.apple.com/cn/app/id1516439349'; </script>";
        }else{
           echo "<script> location.href='https://a.app.qq.com/o/simple.jsp?pkgname=com.paixun.fz'; </script>";
        }

    }

    /**
     * football
     */
    public function football() {
        set_time_limit(0);
        $today = date('Y-m-d H:i:s');
        //1、足球直播数据
        $appKey = 'ty020';
        $body = 'matchType=football';
        $header = array(
            'xcode:'.$appKey
        );
        $url = 'http://www.xsports-live.com:8086/live/sport/getLiveInfo';
        $live_result = https_request($url,$body,'POST',$header);
        $lives = json_decode($live_result,true);
        //2、足球飞鲸数据
        $url = 'http://interface.win007.com/football/today.aspx';
        $data_result = file_get_contents($url);
        $datas = json_decode($data_result,true);
        //--------------------数据组合------------------------
        if($lives['data']){
            foreach ($lives['data'] as $k => $v){
                $teams = explode('VS',$v['teams']);
                $teamsE = explode(' VS ',$v['teamsEn']);
                $c1 = trim($teams[0],' ');
                $c2 = trim($teams[1],' ');
                $e1 = trim($teamsE[0],' ');
                $e2 = trim($teamsE[1],' ');
                $data = [
                    'mv_fid' => 1,
                    'mv_name' => $v['liga'],
                    'mv_home_name_cn' => $c1,
                    'mv_home_name_en' => $e1,
                    'mv_away_name_cn' => $c2,
                    'mv_away_name_en' => $e2,
                    'mv_match_time' => $v['timeFormart'],
                    'mv_live_matchid' => $v['matchId'],
                    'mv_live_url1' => $v['liveUrl'],
                    'mv_live_url2' => $v['liveUrl2'],
                    'mv_live_url3' => $v['liveUrl3'],
                    'mv_live_status' => $v['liveStatus'],
                    'mv_status' => $v['status'],
                    'mv_time' => $today,
                    'mv_update' => $today,
                ];
                $match = Db::table('up_match_live')
                    ->where('mv_live_matchid',$v['matchId'])
                    ->field('mv_id,mv_status')
                    ->find();
                if($match){
                    if($v['status'] != 0){}
                    unset($data['mv_time']);
                    Db::table('up_match_live')->where('mv_id',$match['mv_id'])->update($data);
                }else{
                    Db::table('up_match_live')->insert($data);
                }
            }
        }

        if($datas['matchList']){
            foreach ($datas['matchList'] as $k2 => $v2){
                $roundCn = $v2['roundCn'];
                $sub = (int)$v2['roundCn'] > 0 ? '第' . $roundCn . '轮' : $roundCn;
                $data = [
                    'mf_fid' => 1,
                    'mf_name' => $v2['leagueChsShort'],
                    'mf_name_sub' => $v2['subLeagueChs'] . $sub . $v2['grouping'],
                    'mf_league_id' => $v2['leagueId'],
                    'mf_home_name_cn' => $v2['homeChs'],
                    'mf_home_name_en' => $v2['homeEn'],
                    'mf_home_id' => $v2['homeId'],
                    'mf_away_name_cn' => $v2['awayChs'],
                    'mf_away_name_en' => $v2['awayEn'],
                    'mf_away_id' => $v2['awayId'],
                    'mf_match_time' => $v2['matchTime'],
                    'mf_fj_matchid' => $v2['matchId'],
                    'mf_state' => $v2['state'],
                    'mf_json' => json_encode($v2),
                    'mf_time' => $today,
                    'mf_update' => $today,
                ];
                $match = Db::table('up_match_fj')
                    ->where('mf_fj_matchid',$v2['matchId'])
                    ->field('mf_state,mf_id')
                    ->find();
                if($match){
                    $no = [-1,-10];
                    if(!in_array($match['mf_state'],$no)){
                        unset($data['mf_time']);
                        Db::table('up_match_fj')->where('mf_id',$match['mf_id'])->update($data);
                    }
                }else{
                    Db::table('up_match_fj')->insert($data);
                }
            }
        }

        exit();
    }
    /**
     * basketball
     */
    public function basketball() {
        set_time_limit(0);
        $today = date('Y-m-d H:i:s');
        //1、篮球直播数据
        $appKey = 'ty020';
        $body = 'matchType=basketball';
        $header = array(
            'xcode:'.$appKey
        );
        $url = 'http://www.xsports-live.com:8086/live/sport/getLiveInfo';
        $live_result = https_request($url,$body,'POST',$header);
        $lives = json_decode($live_result,true);
        //2、篮球飞鲸数据
        $url = 'http://interface.win007.com/basketball/today.aspx';
        $data_result = file_get_contents($url);
        $datas = json_decode($data_result,true);
        //--------------------数据组合------------------------
        if($lives['data']){
            foreach ($lives['data'] as $k => $v){
                $teams = explode('VS',$v['teams']);
                $teamsE = explode(' VS ',$v['teamsEn']);
                $c1 = trim($teams[0],' ');
                $c2 = trim($teams[1],' ');
                $e1 = trim($teamsE[0],' ');
                $e2 = trim($teamsE[1],' ');
                $data = [
                    'mv_fid' => 2,
                    'mv_name' => $v['liga'],
                    'mv_home_name_cn' => $c1,
                    'mv_home_name_en' => $e1,
                    'mv_away_name_cn' => $c2,
                    'mv_away_name_en' => $e2,
                    'mv_match_time' => $v['timeFormart'],
                    'mv_live_matchid' => $v['matchId'],
                    'mv_live_url1' => $v['liveUrl'],
                    'mv_live_url2' => $v['liveUrl2'],
                    'mv_live_url3' => $v['liveUrl3'],
                    'mv_live_status' => $v['liveStatus'],
                    'mv_status' => $v['status'],
                    'mv_time' => $today,
                    'mv_update' => $today,
                ];
                $match = Db::table('up_match_live')
                    ->where('mv_live_matchid',$v['matchId'])
                    ->field('mv_id')
                    ->find();
                if($match){
                    unset($data['mv_time']);
                    Db::table('up_match_live')->where('mv_id',$match['mv_id'])->update($data);
                }else{
                    Db::table('up_match_live')->insert($data);
                }
            }
        }

        if($datas['matchList']){
            foreach ($datas['matchList'] as $k2 => $v2){
                $data = [
                    'mf_fid' => 2,
                    'mf_name' => $v2['leagueChs'],
                    'mf_league_id' => $v2['leagueId'],
                    'mf_home_name_cn' => $v2['homeTeamChs'],
                    'mf_home_name_en' => $v2['homeTeamEn'],
                    'mf_home_id' => $v2['homeTeamId'],
                    'mf_away_name_cn' => $v2['awayTeamChs'],
                    'mf_away_name_en' => $v2['awayTeamEn'],
                    'mf_away_id' => $v2['awayTeamId'],
                    'mf_match_time' => $v2['matchTime'],
                    'mf_fj_matchid' => $v2['matchId'],
                    'mf_state' => $v2['matchState'],
                    'mf_json' => json_encode($v2),
                    'mf_time' => $today,
                    'mf_update' => $today,
                ];
                $match = Db::table('up_match_fj')
                    ->where('mf_fj_matchid',$v2['matchId'])
                    ->field('mf_state,mf_id')
                    ->find();
                if($match){
                    $no = [-1,-10];
                    if(!in_array($match['mf_state'],$no)){
                        unset($data['mf_time']);
                        Db::table('up_match_fj')->where('mf_id',$match['mf_id'])->update($data);
                    }
                }else{
                    Db::table('up_match_fj')->insert($data);
                }
            }
        }

        exit();
    }
    //需要一个定时器，可以一天一跑，清除文字直播的数据
    public function clearText()
    {
        $time = time();
        $one_month_ago = date("Y-m-d H:i:s",($time - 3600*24*31));
        Db::table('up_match_talk_fj')
            ->where('mtf_time','<',$one_month_ago)
            ->delete();
        exit();
    }
    //同步文字直播和足球echarts数据
    public function matchText()
    {
        set_time_limit(0);
        //---------------先处理echarts- start ----------------------------
        $url1 = 'http://interface.win007.com/football/detail.aspx';
        $data_result1 = file_get_contents($url1);
        $datas1 = json_decode($data_result1, true);
        if($datas1 && $datas1['technic']){
            foreach ($datas1['technic'] as $k => $v) {
                $have = Db::table('up_match_fj')->where('mf_fj_matchid',$v['matchId'])
                    ->field('mf_id,mf_echarts')
                    ->find();
                if($have && $have['mf_echarts'] != $v['technicCount']){
                    Db::table('up_match_fj')->where('mf_fj_matchid',$v['matchId'])->update([
                        'mf_echarts' => $v['technicCount']
                    ]);
                }

            }
        }
        //---------------先处理echarts-  end  ----------------------------
        $today = date('Y-m-d H:i:s');
        for($i=1;$i<=10;$i++) {
            //1、足球文字数据
            $url1 = 'http://interface.win007.com/football/change.aspx?language=cn';
            $data_result1 = file_get_contents($url1);
            $datas1 = json_decode($data_result1, true);
            if ($datas1 && $datas1['changeList']) {
                foreach ($datas1['changeList'] as $k => $v) {
                    if (trim($v['explain'], ' ')) {
                        $data = [
                            'mtf_fj_matchid' => $v['matchId'],
                            'mtf_content' => $v['explain'],
                            'mtf_time' => date('Y-m-d H:i:s')
                        ];
                        $have = Db::table('up_match_talk_fj')
                            ->where('mtf_fj_matchid', $v['matchId'])
                            ->order('mtf_time desc')
                            ->field('mtf_content')
                            ->find();
                        if (!$have || $have['mtf_content'] != $v['explain']) {
                            Db::table('up_match_talk_fj')->insert($data);
                        }
                    }
                    //------------改比赛实时数据-------
                    Db::table('up_match_fj')->where('mf_fj_matchid',$v['matchId'])->update([
                        'mf_json' => json_encode($v),
                        'mf_update' => date('Y-m-d H:i:s')
                    ]);
                }
            }
            //1、篮球文字数据
            $url1 = 'http://interface.win007.com/basketball/change.aspx?language=cn';
            $data_result1 = file_get_contents($url1);
            $datas1 = json_decode($data_result1, true);
            if ($datas1 && $datas1['changeList']) {
                foreach ($datas1['changeList'] as $k => $v) {
                    if (trim($v['explain'], ' ')) {
                        $data = [
                            'mtf_fj_matchid' => $v['matchId'],
                            'mtf_content' => $v['explain'],
                            'mtf_time' => date('Y-m-d H:i:s')
                        ];
                        $have = Db::table('up_match_talk_fj')
                            ->where('mtf_fj_matchid', $v['matchId'])
                            ->order('mtf_time desc')
                            ->field('mtf_content')
                            ->find();
                        if (!$have || $have['mtf_content'] != $v['explain']) {
                            Db::table('up_match_talk_fj')->insert($data);
                        }
                    }
                    //------------改比赛实时数据-------
                    Db::table('up_match_fj')->where('mf_fj_matchid',$v['matchId'])->update([
                        'mf_json' => json_encode($v),
                        'mf_update' => date('Y-m-d H:i:s')
                    ]);
                }
            }
            sleep(5);
        }
    }
    //组队logo和联赛logo和国家：初始化数据
    public function initFootball()
    {
        set_time_limit(0);
        //1、足球球队数据
        $foot1 = Db::table('up_sport')->where('mww_type',1)->where('sport_fid',1)->find();
        $foot2 = Db::table('up_sport')->where('mww_type',2)->where('sport_fid',1)->find();
        $foot3 = Db::table('up_sport')->where('mww_type',3)->where('sport_fid',1)->find();
        $foot4 = Db::table('up_sport')->where('mww_type',4)->where('sport_fid',1)->find();
        $foot5 = Db::table('up_sport')->where('mww_type',5)->where('sport_fid',1)->find();
        $datas1 = [];
        if(!$foot1){
            $page = 1;
            $url1 = 'http://interface.win007.com/football/team.aspx?page=1';
            $data_result1 = file_get_contents($url1);
            $datas1 = json_decode($data_result1,true);
        }
        if($foot1 && !$foot2){
            $page = 2;
            $url1 = 'http://interface.win007.com/football/team.aspx?page=2';
            $data_result1 = file_get_contents($url1);
            $datas1 = json_decode($data_result1,true);
        }
        if($foot1 && $foot2 && !$foot3){
            $page = 3;
            $url1 = 'http://interface.win007.com/football/team.aspx?page=3';
            $data_result1 = file_get_contents($url1);
            $datas1 = json_decode($data_result1,true);
        }
        if($foot1 && $foot2 && $foot3 && !$foot4){
            $page = 4;
            $url1 = 'http://interface.win007.com/football/team.aspx?page=4';
            $data_result1 = file_get_contents($url1);
            $datas1 = json_decode($data_result1,true);
        }
        if($foot1 && $foot2 && $foot3 && $foot4 && !$foot5){
            $page = 5;
            $url1 = 'http://interface.win007.com/football/team.aspx?page=5';
            $data_result1 = file_get_contents($url1);
            $datas1 = json_decode($data_result1,true);
        }
        if ($datas1 && $datas1['teamList']) {
            foreach ($datas1['teamList'] as $k => $v) {
                $data = [
                    'sport_fid' => 1,
                    'sport_team_id' => $v['teamId'],
                    'sport_name_cn' => $v['nameChs'],
                    'sport_name_en' => $v['nameEn'],
                    'sport_logo' => $v['logo'],
                    'mww_type' => $page
                ];
                Db::table('up_sport')->insert($data);
            }
        }
    }
    public function initBasketball()
    {
        set_time_limit(0);
        $bask = Db::table('up_sport')->where('sport_fid',2)->find();
        if(!$bask){
            //1、篮球球队数据
            $url1 = 'http://interface.win007.com/basketball/team.aspx';
            $data_result1 = file_get_contents($url1);
            $datas1 = json_decode($data_result1,true);
            if ($datas1 && $datas1['TeamList']) {
                foreach ($datas1['TeamList'] as $k => $v) {
                    $data = [
                        'sport_fid' => 2,
                        'sport_team_id' => $v['teamId'],
                        'sport_name_cn' => $v['nameChsShort'],
                        'sport_name_en' => $v['nameEn'],
                        'sport_logo' => $v['logo']
                    ];
                    Db::table('up_sport')->insert($data);
                }
            }
        }

    }
    public function initLeague()
    {
        set_time_limit(0);
        $foot = Db::table('up_sport_team')->where('st_fid',1)->find();
        $bask = Db::table('up_sport_team')->where('st_fid',2)->find();
        if(!$foot){
            //1、足球联赛数据
            $url1 = 'http://interface.win007.com/football/league.aspx';
            $data_result1 = file_get_contents($url1);
            $datas1 = json_decode($data_result1,true);
            if ($datas1 && $datas1['leagueList']) {
                foreach ($datas1['leagueList'] as $k => $v) {
                    $data = [
                        'st_fid' => 1,
                        'st_league_id' => $v['leagueId'],
                        'st_name_en' => $v['nameEn'],
                        'st_name_cn' => $v['nameChsShort'],
                        'st_logo' => $v['leagueLogo'],
                        'st_country' => $v['countryCn'],
                        'st_country_en' => $v['countryEn']
                    ];
                    Db::table('up_sport_team')->insert($data);
                }
            }
        }
        if(!$bask){
            //1、篮球联赛数据
            $url1 = 'http://interface.win007.com/basketball/league.aspx';
            $data_result1 = file_get_contents($url1);
            $datas1 = json_decode($data_result1,true);
            if ($datas1 && $datas1['list']) {
                foreach ($datas1['list'] as $k => $v) {
                    $data = [
                        'st_fid' => 2,
                        'st_league_id' => $v['leagueId'],
                        'st_name_en' => $v['nameEnShort'],
                        'st_name_cn' => $v['nameChsShort'],
                        'st_logo' => $v['logo'],
                        'st_country' => $v['countryCn'],
                        'st_country_en' => $v['countryEn']
                    ];
                    Db::table('up_sport_team')->insert($data);
                }
            }
        }
    }
    //-------------------------------------------------------
    //-------------------------------------------------------
    /**
     * 一天跑一次（每天的凌晨1点跑，把记录清除掉）
     */
    public function timelogdel(){
        set_time_limit(0);
        //清除
        $dir = ROOT_PATH . 'runtime' . DS . 'log';
        $this->deldir($dir);
        exit();
    }
    public function deldir($dir)
    {
        $dh = opendir($dir);
        while ($file = readdir($dh)) {
            if ($file != "." && $file != "..") {
                $fullpath = $dir . '/' . $file;
                if (!is_dir($fullpath)) {
                    unlink($fullpath);
                } else {
                    $this->deldir($fullpath);
                }
            }
        }
        closedir($dh);
        if (rmdir($dir)) {
            return true;
        } else {
            return false;
        }
    }
}